package com.corx.network.core.listener;

import com.corx.network.core.model.ReceivedMessage;
import com.corx.network.core.storage.Storage;

import java.util.function.Function;


/**
 * @author gx
 * @version 1.0.0
 */
public abstract class ReceivedMessageListener<T extends ReceivedMessage, R extends ReceivedMessage> implements Function<T, R> {

    private Storage storage;

    public void setStorage(Storage storage) {
        this.storage = storage;
    }

    @Override
    public R apply(T t) {
        R handler = this.doHandler(t);
        if (handler != null) {
            if ( storage != null){
                storage.doHandler(handler);
            }
        }
        return handler;
    }

    protected abstract R doHandler(T t);

}
